package com.example.asapp.demo.chat;

import com.alibaba.fastjson.JSON;
import com.example.asapp.base.BaseService;
import com.example.asapp.socket.WebSocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class ChatService{

    @Autowired
    ChatDao chatDao;

    @Autowired
    BaseService baseService;

    @Autowired
    WebSocketServer socketServer;

    public List<Chat> findAll() {
        return chatDao.findAll();
    }

    public Chat saveOne(Chat chat) {
        Chat chat1=chatDao.save(chat);
        ChatChangeSockInfo chatChangeSockInfo=new ChatChangeSockInfo(baseService.getBaseUserInfo(),chat1,"save_chat_over");
        socketServer.sendMessageToAll(JSON.toJSONString(chatChangeSockInfo));
        return chat1;
    }

    public void deleteByPrimary(String id) {
      chatDao.deleteById(id);
    }
}
